package com.eonmind.ai.core.draw.schedule.handler;

import com.eonmind.ai.core.draw.model.AiDrawTaskModel;
import com.eonmind.ai.core.draw.model.AiDrawTaskResModel;
import com.eonmind.ai.core.draw.platform.mdJounery.MdDrawService;
import com.eonmind.ai.core.draw.platform.mdJounery.model.MdCommandRequest;
import com.eonmind.ai.mapper.AiDrawMapper;
import com.eonmind.common.contract.application.redis.service.RedisService;
import com.eonmind.common.contract.enums.ai.AiArtworkStatusEnum;
import com.eonmind.common.provider.entity.ai.AiArtwork;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Xujie
 * @since 2025/5/31 21:41
 **/
@Slf4j
@Component
public class MjSubmitTaskHandler {


    @Resource
    private MdDrawService mdDrawService;

    @Resource
    private AiDrawMapper aiDrawMapper;

    @Resource
    private RedisService redisService;
    private final String submitLimitKey = "task:submitLimit:";

    public void submitTaskList(List<AiArtwork> aiArtworkList) {
        if (ObjectUtils.isEmpty(aiArtworkList)) {
            log.info("MJ绘画任务列表为空，跳过执行");
            return;
        }
        aiArtworkList.forEach(aiArtwork -> {
            try {
                if (ObjectUtils.isEmpty(aiArtwork.getTaskId())) {
                    String prompt = aiArtwork.getPrompt();
                    AiDrawTaskResModel aiDrawTaskResModel = null;
                    Long artworkId = aiArtwork.getId();
                    Long incr = redisService.incr(submitLimitKey + artworkId, 1L);
                    if (incr > 10) {
                        log.info("MJ绘画任务提交限制，跳过执行");
                        aiArtwork.setStatus(AiArtworkStatusEnum.GENERATED_FAILED.getCode());
                    } else {
                        if (prompt.contains("Enlarge")) {
                            String[] split = prompt.split("_");
                            if (ObjectUtils.isEmpty(split) || split.length != 3) {
                                log.info("MJ绘画任务提交失败，prompt格式错误");
                            } else {
                                String taskId = split[1];
                                String customId = split[2];
                                MdCommandRequest mdCommandRequest = new MdCommandRequest();
                                mdCommandRequest.setTaskId(taskId);
                                mdCommandRequest.setCustomId(customId);
                                aiDrawTaskResModel = mdDrawService.executeCommandTask(mdCommandRequest);
                            }
                        } else {
                            aiDrawTaskResModel = mdDrawService.submitDrawTask(buildMjTask(aiArtwork));

                        }
                        if (aiDrawTaskResModel != null) {
                            aiArtwork.setTaskId(aiDrawTaskResModel.getTaskId());
                            aiArtwork.setPlatformId(aiDrawTaskResModel.getPlatformId());
                        }
                        aiArtwork.setStatus(AiArtworkStatusEnum.PENDING_GENERATION.getCode());
                    }

                }
            } catch (Exception e) {
                log.error("定时执行MJ绘画任务失败：{}", aiArtwork.getId(), e);
            }
        });
        aiDrawMapper.updateBatchByIds(aiArtworkList);
    }

    private AiDrawTaskModel buildMjTask(AiArtwork aiArtwork) {
        AiDrawTaskModel aiDrawTaskModel = new AiDrawTaskModel();
        aiDrawTaskModel.setPrompt(aiArtwork.getPrompt());
        return aiDrawTaskModel;
    }
}
